/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package testAgent.behaviours;

import jade.core.Agent;
import jade.core.behaviours.DataStore;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import jade.proto.AchieveREInitiator;
import udes.ds.agent.Equation;

/**
 * @author Cody Stoutenburg
 * 
 */
public class RequestEquationSolveBehaviours extends AchieveREInitiator {
	private static final long serialVersionUID = 7836210876019088130L;

	public static final int AGREE = 1;
	public static final int REFUSE = 0;
	public static final int NO_RESPONSE = -1;

	public static final int RESULT_FOUND = 2;
	public static final int CORRUPT_RESPONSE = -2;

	private final String responseKey;
	private final String resultKey;

	/**
	 * @param a
	 * @param cfp
	 */
	public RequestEquationSolveBehaviours(Agent a, ACLMessage cfp,
			DataStore shared, String response, String result) {
		super(a, cfp);

		this.setDataStore(shared);
		responseKey = response;
		resultKey = result;

		this.getDataStore().put(response, NO_RESPONSE);
	}

	@Override
	protected void handleAgree(ACLMessage agree) {
		this.getDataStore().put(responseKey, AGREE);
	}

	@Override
	protected void handleRefuse(ACLMessage refuse) {
		this.getDataStore().put(responseKey, REFUSE);
	}

	@Override
	protected void handleInform(ACLMessage inform) {
		try {
			Equation result = (Equation) inform.getContentObject();
			getDataStore().put(responseKey, RESULT_FOUND);
			getDataStore().put(resultKey, result);
		} catch (UnreadableException e) {
			getDataStore().put(responseKey, CORRUPT_RESPONSE);
			e.printStackTrace();
		}
	}

}